package com.huike.pmps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huike.pmps.dao.SupplierAttachmentDao;
import com.huike.pmps.model.constants.GlobalConstants;
import com.huike.pmps.model.entity.SupplierAttachment;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.SupplierAttachmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Service
public class SupplierAttachmentServiceImpl implements SupplierAttachmentService {
    @Autowired
    SupplierAttachmentDao supplierAttachmentDao;

    @Override
    public Result selectByCondition(Integer supplierId) {
        QueryWrapper<SupplierAttachment> queryWrapper = new QueryWrapper<>();
        if (supplierId != null) {
            queryWrapper.eq("supplier_id", supplierId);
        }

        //附件(供应商视频样片)
        List<SupplierAttachment> attachmentVideo = new ArrayList<>();
        //附件(供应商音频文件)
        List<SupplierAttachment> attachmentAudio = new ArrayList<>();
        //附件(供应商效果图)
        List<SupplierAttachment> attachmentImage = new ArrayList<>();
        //附件(相关文档)
        List<SupplierAttachment> attachmentDoc = new ArrayList<>();

        List<SupplierAttachment> supplierAttachments = supplierAttachmentDao.selectList(queryWrapper);

        //1视频样片、2音频文件、3效果图、4相关文档
        if (supplierAttachments.size() > 0) {
            for (SupplierAttachment supplierAttachment : supplierAttachments) {
                if ("1".equals(supplierAttachment.getType())) {
                    attachmentVideo.add(supplierAttachment);
                }
                if ("2".equals(supplierAttachment.getType())) {
                    attachmentAudio.add(supplierAttachment);
                }
                if ("3".equals(supplierAttachment.getType())) {
                    attachmentImage.add(supplierAttachment);
                }
                if ("4".equals(supplierAttachment.getType())) {
                    attachmentDoc.add(supplierAttachment);
                }
            }
        }
        Object resultList = new Object[]{attachmentVideo, attachmentAudio, attachmentImage, attachmentDoc};
        return Result.success(resultList);
    }

    /**
     * 获取附件
     *
     * @param supplierId
     * @return
     */
    @Override
    public Result selectByCondition(Integer supplierId, Integer type) {
        QueryWrapper<SupplierAttachment> queryWrapper = new QueryWrapper<>();
        if (supplierId != null) {
            queryWrapper.eq("supplier_id", supplierId);
        }
        if (type != null) {
            queryWrapper.eq("type", type);
        }

    /*    //附件(供应商视频样片)
        List<SupplierAttachment> attachmentVideo = new ArrayList<>();
        //附件(供应商音频文件)
        List<SupplierAttachment> attachmentAudio = new ArrayList<>();
        //附件(供应商效果图)
        List<SupplierAttachment> attachmentImage = new ArrayList<>();
        //附件(相关文档)
        List<SupplierAttachment> attachmentDoc = new ArrayList<>();

        List<SupplierAttachment> supplierAttachments = supplierAttachmentDao.selectList(queryWrapper);

        if (supplierAttachments.size() > 0) {
            for (SupplierAttachment supplierAttachment : supplierAttachments) {
                if (GlobalConstants.SUPPLIER_ATTACHMENT_TYPE_VIDEO.equals(supplierAttachment.getType())) {
                    attachmentVideo.add(supplierAttachment);
                }
                if (GlobalConstants.SUPPLIER_ATTACHMENT_TYPE_AUDIO.equals(supplierAttachment.getType())) {
                    attachmentAudio.add(supplierAttachment);
                }
                if (GlobalConstants.SUPPLIER_ATTACHMENT_TYPE_IMAGE.equals(supplierAttachment.getType())) {
                    attachmentImage.add(supplierAttachment);
                }
                if (GlobalConstants.SUPPLIER_ATTACHMENT_TYPE_DOC.equals(supplierAttachment.getType())) {
                    attachmentDoc.add(supplierAttachment);
                }
            }
        }
        Object resultList = new Object[]{attachmentVideo, attachmentAudio, attachmentImage, attachmentDoc};*/
        return Result.success(supplierAttachmentDao.selectList(queryWrapper));
    }

    /**
     * 删除附件
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        supplierAttachmentDao.deleteById(id);
        return Result.success();
    }

    /**
     * 插入附件
     *
     * @param supplierAttachment
     * @return
     */
    @Override
    public Result insertOne(SupplierAttachment supplierAttachment) {
        supplierAttachmentDao.insert(supplierAttachment);
        return Result.success("上传成功", supplierAttachment);
    }
}
